/**
 * 
 */
package com.gmail.blackdog1987.carcare.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gmail.blackdog1987.carcare.dao.UserDao;
import com.gmail.blackdog1987.carcare.entity.TUser;
import com.gmail.blackdog1987.carcare.service.UserService;
import com.gmail.blackdog1987.carcare.util.ErrorCode;

/**
 * 
 * @author 董国超
 *
 */
@Service("userService")
public class UserServiceImpl implements UserService {
	
	@Autowired
	private UserDao userDao;
//	private ScoreService scoreService;
	
//	static Logger log = Logger.getLogger(UserServiceImpl.class);
	
	public Map<String, Object> doLogin(Map<String, String> filter) {
		Map<String, Object> result = new HashMap<String, Object>();
		
		result.put("code", ErrorCode.NO);
		
		String username = filter.get("username");
		String pwd = filter.get("pwd");
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("account", username);
		param.put("pwd", pwd);
		List<TUser> users = userDao.find(param);
		if(users==null||users.size()==0){ // 用户名或密码错误
			result.put("code", ErrorCode.LOGIN_PWD);
		}else{
			result.put("result", users.get(0));
		}
		return result;
	}

	@Override
	public Map<String, Object> doRegister(Map<String, String> filter) {
		Map<String, Object> result = new HashMap<String, Object>();
		
		result.put("code", ErrorCode.NO);
		
		String username = filter.get("username");
		String pwd = filter.get("pwd");
		
		List<TUser> users = userDao.findBy("account", username);
		if(users!=null&&users.size()!=0){ 
			result.put("code", ErrorCode.REG_EXISTS);
			return result;
		}
		TUser TUser = new TUser();
//		TUser.setAccount(username);
//		TUser.setPwd(pwd);
		
		return result;
	}

	@Override
	public Map<String, Object> doEditInfo(Map<String, String> filter) {
		Map<String, Object> result = new HashMap<String, Object>();
		
		result.put("code", ErrorCode.NO);
		
		String username = filter.get("username");
		String pwd = filter.get("pwd");
		
		List<TUser> users = userDao.findBy("account", username);
		if(users!=null&&users.size()==0){ 
			result.put("code", ErrorCode.USEREDIT_NOTEXISTS);
			return result;
		}
		TUser TUser = users.get(0);
		
		return result;
	}
}
